package com.archermind.boc.dal.business.support;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import com.archermind.boc.Logger;
import com.archermind.boc.dal.business.file.IImageHelper;
import com.archermind.boc.dal.business.file.impl.ImageHelper;
import com.archermind.boc.domain.Biz;
import com.archermind.boc.domain.IQ;
import com.archermind.boc.domain.Image;
import com.archermind.boc.domain.Message;
import com.archermind.boc.domain.Session;
import com.archermind.boc.domain.TextMessage;
import com.archermind.boc.domain.User;
import com.archermind.boc.ehcache.UserCache;
import com.archermind.boc.ehcache.UserEhcacheImpl;
import com.archermind.sessionpool.BizSessionPool;

public class DispatchSupport {
	private static Logger log = Logger.getLogger(DispatchSupport.class
			.getName());
	private static final String KEY_SCREEN = "screen";
	private static final String KEY_IMAGE = "image";
	public static Biz getBIZ(HashMap<String, List<String>> map)
	{
		Biz biz = new Biz();
		biz.setLog("New link");
		biz.setCreateTime(new Date());
		try {
//			if(message.getTo()!=null)
//			{
//				biz.setToBankNum(Integer.parseInt(message.getTo()));
//			}
//			if(message.getFrom()!=null)
//			{
//				biz.setFromBankNum(Integer.parseInt(message.getFrom()));
//			}
			if(map.get("bizType").get(0)!=null && !"".equals(map.get("bizType").get(0))){
				biz.setBizTypeID(map.get("bizType").get(0));
//				biz.setBizType(Integer.parseInt(map.get("bizType").get(0)));
			}
		}
		 catch (Exception e) {
			log.error("Persistence: Message get biz fail !", e);
			return null;
		}
		return biz;
	}

	public static void setMessage(UserCache userCache,IQ iq, String jingbanSessionID,Biz biz,int sessionTimeOut) {
		iq.setId(iq.getId());
		iq.setSessionID(jingbanSessionID);
		iq.setFrom(iq.getTo());//from是服务器地址,第一次发送的to就是服务器地址
		UserEhcacheImpl userehcacheimpl = new UserEhcacheImpl();
		UserCache jingbanuserCache = userehcacheimpl.getUserCachebySessionId(jingbanSessionID);//找到经办端的user
		iq.setTo(jingbanuserCache.getLoginName());
		HashMap<String, List<String>> map = new HashMap<String, List<String>>();
		setMap("bizid",map,biz.getBizid());
		setMap("bizType",map,biz.getBizTypeID());
		setMap("toBankNum",map,userCache.getLoginName());
		setMap("fromBankNum",map,jingbanuserCache.getLoginName());
		setMap("toNetNum",map,userCache.getNetworkIdentify());
		setMap("fromNetNum",map,jingbanuserCache.getNetworkIdentify());
		setMap("toRealName",map,userCache.getRealName());
		setMap("fromRealName",map,jingbanuserCache.getRealName());
		setMap("sessionTimeOut",map,sessionTimeOut);
		biz.setFromBankNum(jingbanuserCache.getUserid());
		iq.getSession().setX(map);
	}
	
	@SuppressWarnings("unchecked")
	public static void setMap(String key,HashMap<String, List<String>> map,String object)
	{
		List list = new ArrayList<String>();
		list.add(object);
		map.put(key, list);
	}
	@SuppressWarnings("unchecked")
	public static void setMap(String key,HashMap<String, List<String>> map,int object)
	{
		List list = new ArrayList<String>();
		list.add(String.valueOf(object));
		map.put(key, list);
	}

	public static void getXdataByText(Biz biz, Message message) {
		TextMessage text = new TextMessage();
		text.setCreateTime(new java.util.Date());
		text.setTextMessage(message.getBody());
		text.setBiz(biz);
		biz.getTextMessage().add(text);
	}

	public static void getXdataByImage(Biz biz, Message message,
			HashMap<String, List<String>> map) {
		IImageHelper imageHelper = ImageHelper.getInstance();
		if(map.get(KEY_SCREEN)!=null)
		{
			for (String screen : map.get(KEY_SCREEN)) {
				String path = imageHelper.write(screen, message.getSessionID());
				Image image = new Image();
				image.setPath(path);
				image.setType(KEY_SCREEN);
				image.setBiz(biz);
				biz.getImage().add(image);
			}
		}
		if(map.get(KEY_IMAGE)!=null)
		{
			for (String imageKey : map.get(KEY_IMAGE)) {
				String path = imageHelper.write(imageKey, message.getSessionID());
				Image image = new Image();
				image.setPath(path);
				image.setType(KEY_IMAGE);
				image.setBiz(biz);
				biz.getImage().add(image);
			}
		}
	}

	public static Biz getBizByUserCache(Message message,
			UserEhcacheImpl userEhcache) {
		Biz biz = null;
		UserCache userCache = userEhcache.getUserCachebySessionId(message
				.getSessionID());
		if (userCache != null) {
			biz = userCache.getBiz();
		} else {
			log.error("getUserCachebySessionId is null !");
		}
		return biz;
	}
	public static Biz getBizByUserCache(IQ iq,
			UserEhcacheImpl userEhcache) {
		Biz biz = null;
		UserCache userCache = userEhcache.getUserCachebySessionId(iq
				.getSessionID());
		if (userCache != null) {
			biz = userCache.getBiz();
		} else {
			log.error("getUserCachebySessionId is null !");
		}
		return biz;
	}

	public static IQ setSessionOutMessage(User grant, User operator,
			String sessionID, Biz biz) {
		IQ iq = new IQ();

		iq.setSessionID(sessionID);

		if (grant != null) {
			iq.setTo(grant.getLoginName());
		}

		if (operator != null) {
			iq.setFrom(operator.getLoginName());
		}

		HashMap<String, List<String>> mapX = new HashMap<String, List<String>>();
		
		setMap("bizid", mapX, biz.getBizid());
		setMap("bizType", mapX, biz.getBizTypeID());
		setMap("toBankNum", mapX, biz.getToBankNum());
		setMap("fromBankNum", mapX, biz.getFromBankNum());
		setMap("status", mapX, biz.getStatus());

		iq.setSession(new Session());
		iq.getSession().setX(mapX);
		return iq;
	}

	public static IQ setManualMessage(String sessionID, Biz biz) {
		IQ iq = new IQ();
		iq.setSessionID(sessionID);
		
		HashMap<String, List<String>> mapX = new HashMap<String, List<String>>();
		setMap("bizid", mapX, biz.getBizid());
		setMap("bizType", mapX, biz.getBizTypeID());
		setMap("sessionTimeOut", mapX, String.valueOf(-1));
		iq.setSession(new Session());
		iq.getSession().setX(mapX);
		
		return iq;
		
	}

	public static void closeBiz(IQ iq, UserEhcacheImpl userEhcache,String obj) {
		Biz biz = DispatchSupport.getBizByUserCache(iq, userEhcache);
		//UserCache ManagersUserCache = userEhcache.getUserCacheById(biz.getFromBankNum());
		//iq.setSessionID(ManagersUserCache.getSessionID());
		//bocSocketWriter.write(message);//发送给经办端message
		//IQ iq = new IQ();
		//iq.setSessionID(ManagersUserCache.getSessionID());
		//Session session = new Session();
		//HashMap<String, List<String>> IQmap = new HashMap<String, List<String>>();
		//DispatchSupport.setMap("action", IQmap, "close");
		//session.setX(IQmap);
		//iq.setSession(session);
		//bocSocketWriter.write(iq);//发送给经办端iq
		//if(biz.getToBankNum()!=0){
			//UserCache AuthorizedUserCache = userEhcache.getUserCacheById(biz.getToBankNum());
			//iq.setSessionID(AuthorizedUserCache.getSessionID());
			//bocSocketWriter.write(iq);//发送给授权端iq
		//}
		
		BizSessionPool.grantDone(biz.getBizid(), obj);
		
	}
	
	
	
	

}
